Util.onResizeEnd(drawChart);
google.load("visualization", "1", {
	packages : [ "corechart" ]
});

var pollutionData = null;

var substance_level_map = {
	no2 : [ 135, 335, 535, 601 ],
	o3 : [ 67, 141, 214, 241 ],
	so2 : [ 178, 444, 888, 1065 ],
	pm10 : [ 24, 48, 65, 71 ],
	pm2p5 : [ 34, 67, 92, 101 ]
};

var substance_level_map_object = {
	no2 : [ {
		v : 135,
		f : "Low"
	}, {
		v : 335,
		f : "Moderate"
	}, {
		v : 535,
		f : "High"
	}, {
		v : 601,
		f : "Very High"
	} ],
	o3 : [ {
		v : 67,
		f : "Low"
	}, {
		v : 141,
		f : "Moderate"
	}, {
		v : 214,
		f : "High"
	}, {
		v : 241,
		f : "Very High"
	} ],
	so2 : [ {
		v : 178,
		f : "Low"
	}, {
		v : 444,
		f : "Moderate"
	}, {
		v : 888,
		f : "High"
	}, {
		v : 1065,
		f : "Very High"
	} ],
	pm10 : [ {
		v : 24,
		f : "Low"
	}, {
		v : 48,
		f : "Moderate"
	}, {
		v : 65,
		f : "High"
	}, {
		v : 71,
		f : "Very High"
	} ],
	pm2p5 : [ {
		v : 34,
		f : "Low"
	}, {
		v : 67,
		f : "Moderate"
	}, {
		v : 92,
		f : "High"
	}, {
		v : 101,
		f : "Very High"
	} ]
};

/**
 * 
 * 
 * @param latitude
 * @param longitude
 * @param address
 */
function doOnLoad(latitude, longitude, gridId, address) {
	updateSubstance(1, gridId);
	getAddress(latitude, longitude, function(lat, lng, gridId, address){
		$("#address").html(address.formatted_address);	
	});
	
	
	$("#select-substance").change(function() {
		drawChart();
	});

	$("#select-time-range").change(function() {
		updateSubstance($(this).val(), gridId);
	});
}

function updateSubstance(days, gridId) {
	$.ajax({
		type : "GET",
		url : "data/historical-data-for-location",
		dataType : "json",
		data : {
			days : days || 1,
			gridId : gridId
		}
	}).done(function(json) {
		updatePollutionData(json[0]);
		pollutionData = json;
		drawChart();
	});
}

function drawChart() {
	var currentSubstance = $("#select-substance").val();
	var currentTimeRange = $("#select-time-range").val();
	var data = [ [ currentSubstance, "Level"] ];

	var obs = pollutionData;
	for (var i = 0; i < obs.length; i++) {
		var d = Date.parse(obs[i].timestamp);
		data.push([ new Date(d), obs[i][currentSubstance] ]);
	}
	data = google.visualization.arrayToDataTable(data);
	
	var titles = {
			"1":"Today's",
			"7":"Last 7 day's",
			"30":"Last 30 day's"
	};

	var options = {
		title : currentSubstance.toUpperCase() + ' Measurement',
		hAxis : {
			format : 'HH:MM, MMM d',
			title : titles[currentTimeRange] + ' Data',
			titleTextStyle : {
				color : '#333'
			},
			gridlines : {
				color : '#cccccc'
			},

		},
		vAxis : {
			minValue : 0,
			gridlines : {
				color : '#cccccc',
				count : 4
			},
			ticks : substance_level_map_object[currentSubstance]
		}
	};

	var chart = new google.visualization.AreaChart(document
			.getElementById('chart_div'));
	chart.draw(data, options);
}

function updatePollutionData(json) {
	var atRisk = true;
	var banding = Pollution.getAirQualityBanding(Pollution.getAQI(json));
	var messages = Pollution.getHealthMessage(banding, atRisk);
	var pollutionClass = Pollution.getAirQualityClass(json);
	$("#health-message").html("<p>" + messages[0] + "</p>").addClass(
			pollutionClass);

	$("#air-quality-indicator").find(".dark-" + pollutionClass).append(
			"<span id='circle'></span>");
}
